﻿#region

using System.Collections.Generic;
using System.Diagnostics;
using System.Web.Mvc;
using AutoMapper;
using BTL.OnlineTestingSocial.Application.Facade;
using BTL.OnlineTestingSocial.Application.Web.Infrastructure.ViewModels;
using BTL.OnlineTestingSocial.Domain.TestingEngine;
using BTL.OnlineTestingSocial.Services.TestingEngine.Contract;
using BTL.Technology.Framework;

#endregion

namespace BTL.OnlineTestingSocial.Application.Web.Infrastructure.ServiceFacades
{
    public class OnlineTestingEngineFacade : BaseServiceFacade, IOnlineTestingEngineFacade
    {
        private readonly ITestingEngineService _testingEngineService;

        public OnlineTestingEngineFacade()
            : this(DependencyResolver.Current.GetService<ITestingEngineService>())
        {
        }

        public OnlineTestingEngineFacade(ITestingEngineService testingEngineService)
        {
            _testingEngineService = testingEngineService;
        }

        #region IOnlineTestingEngineFacade Members

        public IEnumerable<Category> GetCategories()
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            return _testingEngineService.GetCategories();
        }

        public IEnumerable<QuestionGroupItemViewModel> GetPublicQuestionGroups(int userId, int pageSize, int pageNum)
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            var res = _testingEngineService.GetPublicQuestionGroups(userId, pageSize, pageNum);

            var lst = new List<QuestionGroupItemViewModel>();

            foreach (var questionGroupItemViewModel in res)
            {
                lst.Add(Mapper.Map<QuestionGroupItem, QuestionGroupItemViewModel>(questionGroupItemViewModel));
            }

            return lst;
        }

        public IEnumerable<QuestionGroupItemViewModel> GetPrivateQuestionGroups(int userId, int pageSize, int pageNum)
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            var res = _testingEngineService.GetPrivateQuestionGroups(userId, pageSize, pageNum);

            var lst = new List<QuestionGroupItemViewModel>();

            foreach (var questionGroupItemViewModel in res)
            {
                lst.Add(Mapper.Map<QuestionGroupItem, QuestionGroupItemViewModel>(questionGroupItemViewModel));
            }

            return lst;
        }

        public bool InsertComment(string content, int? questionDetailId, int? questionGroupId, string createBy)
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            return _testingEngineService.InsertComment(content, questionDetailId, questionGroupId, createBy);
        }

        public bool InsertLike(bool isDislike, int? questionDetailId, int? questionGroupId, string createBy)
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            return _testingEngineService.InsertLike(isDislike, questionDetailId, questionGroupId, createBy);
        }

        public bool DeleteQuestionGroup(string ids)
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            return _testingEngineService.DeleteQuestionGroup(ids);
        }

        /// <summary>
        /// Get question group detail
        /// </summary>
        /// <param name="questionGroupId"></param>
        /// <returns></returns>
        public QuestionGroupItem GetQuestionGroupDetail(int questionGroupId)
        {
            Trace.WriteLineIf(ServiceFacadesTraceSwitch.Enabled, "Begin");

            return _testingEngineService.GetQuestionGroupDetail(questionGroupId);
        }

        #endregion
    }
}